<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <OrgTree class="w-1/5 xl:w-1/6" @select="handleSelect" />
    <BasicTable @register="registerTable" class="w-4/5 xl:w-5/6" :searchInfo="searchInfo">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增用户</a-button>
      </template>

      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="[
            {
              icon: 'material-symbols-light:lock-reset-rounded',
              tooltip: '角色授权',
              onClick: handleAssign.bind(null, record),
            },
          ]" />
        </template>
      </template>
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess" />
    <AccountAssignModal @register="registerAssignModal" @success="handleAssignSuccess" />
  </PageWrapper>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { BasicTable, useTable, TableAction } from '@/components/Table'
import { PageWrapper } from '@/components/Page'
import OrgTree from './OrgTree.vue'
import { useModal } from '@/components/Modal'
import AccountModal from './AccountModal.vue'
import AccountAssignModal from './AccountAssignModal.vue'
import { columns, searchFormSchema } from './account.data'
import { deleteUser, postUserList, resetPassword } from '@/api/sys/user'

defineOptions({ name: 'AccountManagement' })

const [registerModal, { openModal }] = useModal()
const [registerAssignModal, { openModal: openAssignModal }] = useModal()
const searchInfo = reactive<Recordable>({})
const [registerTable, { reload, updateTableDataRecord }] = useTable({
  title: '用户列表',
  api: postUserList,
  rowKey: 'id',
  size: 'small',
  columns,
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
    autoSubmitOnEnter: true,
  },
  useSearchForm: true,
  showTableSetting: false,
  bordered: true,
  handleSearchInfoFn(info) {
    // console.log('handleSearchInfoFn', info)
    return info
  },
  actionColumn: {
    width: 100,
    title: '操作',
    dataIndex: 'action',
    // slots: { customRender: 'action' },
  },
})

function handleCreate() {
  openModal(true, {
    isUpdate: false,
  })
}

function handleAssign(record: Recordable) {
  // console.log(record)
  openAssignModal(true, {
    record,
    isUpdate: true,
  })
}

function handleEdit(record: Recordable) {
  // console.log(record)
  openModal(true, {
    record,
    isUpdate: true,
  })
}

async function handleDelete(record: Recordable) {
  await deleteUser(record.userId)
  reload();
}

function handleSuccess({ isUpdate, values }) {
  if (isUpdate) {
    // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
    updateTableDataRecord(values.id, values)
  } else {
    reload()
  }
}

function handleAssignSuccess() {
  // console.log('handle assign success');
  reload();
}

function handleSelect(orgId = '') {
  searchInfo.orgId = orgId
  reload()
}

async function handleResetPassword(record: Recordable) {
  await resetPassword(record.userId)
}

</script>
